$(document).ready(function(){
    if($.cookie('postback')!=null) {
        populateFields();
    }
    $.ajaxSetup({
        async:false
    });
    $("#registraciona_forma").validate({
        rules: {
            username: {
                maxlength: 25
            },
            password: {
                minlength: 4
            }
        },
        submitHandler: function(form) {
            if (validAll()) {
                form.submit();
            }
        }
    });
});


function validAll() {
    var okUsername = checkIfExistUsername();
    var okEmail1 = checkIfExistEmail()
    var okPassword = checkIfPasswordsMatch();
    var okEmail2 = checkIfEmailValid();
    var okAgreement = checkAgreement();
    var notAlreadyRegistered = checkIfJustRegisterWithSession();
    if (okUsername && okPassword && okEmail1 && okEmail2 && okAgreement && notAlreadyRegistered) {
        return true;
    }
    else {
        return false;
    }
}

function checkIfExistUsername() {
    usernamestate = false;
    var username= $('#username').val();
    $.post("?handler=hregistracija", {
        action: 'checkUsername',
        username: username
    },
    function(data) {
        if ($.trim(data)=='true') {
            alert('Korisnicko ime koje ste uneli već postoji');
            usernamestate = false;
        } else {
            //alert('Username NOT Exist. Bravo');
            usernamestate = true;
        }
    });
    return usernamestate;
}

function checkIfExistEmail() {
    emailstate = false;
    var email= $('#email').val();
    $.post("?handler=hregistracija", {
        action: 'checkEmail',
        email: email
    },
    function(data) {
        if ($.trim(data)=='true') {
            alert('Email koji ste uneli već postoji!');
            emailstate =  false;
        } else {
            //alert('Email NOT Exist. Bravo');
            emailstate =  true;
        }
    });
    return emailstate;
}

function checkIfPasswordsMatch() {
    var pass1 = $('#password').val();
    var pass2 = $('#password_repeat').val();
    if (pass1==pass2) {
        //alert('Password matched');
        return true;
    }
    else {
        alert('Password is not match');
        return false;
    }
}

//Need another check for smf
function checkIfEmailValid() {
    var email = $('#email').val();
    var re = /^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$/;
    if(re.test(email)&&email.length<256) {
        //alert('Email je validan');
        return true;
    } else {
        alert('Email nije validan!');
        return false;
    }
}


function checkIfJustRegisterWithSession() {
    registerstate=false;
    $.post("?handler=hregistracija", {
        action: 'checkIfJustRegisterWithSession'
    },
    function(data) {
        if ($.trim(data)=='alreadyRegistered') {
            alert('Već ste se registrovali!');
            registerstate = false;
        } else {
            registerstate = true;
        }
    });
    return registerstate;
}

function checkAgreement() {
    if (!$('#pravila').is(':checked')) {
        alert('Morate da se slažete sa uslovima korišćenja!')
        return false;
    } else {
        return true;
    }
}

function populateFields() {
        //$('#ime').val(decodeURIComponent($.cookie('ime')));
        if ($.cookie('ime')!=null) $('#ime').val($.cookie('ime').replace(/\+/,' '));
        if ($.cookie('username')!=null) $('#username').val($.cookie('username'));
        if ($.cookie('password')!=null) $('#password').val($.cookie('password'));
        if ($.cookie('email')!=null) $('#email').val($.cookie('email'));
        if ($.cookie('mesec_datum_rodjenja')!=null) $('#mesec_datum_rodjenja').val($.cookie('mesec_datum_rodjenja'));
        if ($.cookie('dan_datum_rodjenja')!=null) $('#dan_datum_rodjenja').val($.cookie('dan_datum_rodjenja'));
        if ($.cookie('godina_datum_rodjenja')!=null) $('#godina_datum_rodjenja').val($.cookie('godina_datum_rodjenja'));
        if ($.cookie('grad')!=null) $('#grad').val($.cookie('grad').replace(/\+/,' '));
        if ($.cookie('drzava')!=null) $('#drzava').val($.cookie('drzava'));
        if ($.cookie('telefon')!=null) $('#telefon').val($.cookie('telefon'));
        if ($.cookie('www')!=null) $('#www').val($.cookie('www'));
        if ($.cookie('studio')!=null) $('#studio').val($.cookie('studio').replace(/\+/,' '));
        if ($.cookie('adresa_studija')!=null) $('#adresa_studija').val($.cookie('adresa_studija').replace(/\+/,' '));
        if ($.cookie('telefon_studio')!=null) $('#telefon_studio').val($.cookie('telefon_studio'));
        if ($.cookie('email_studio')!=null) $('#email_studio').val($.cookie('email_studio'));
        if ($.cookie('www_studio')!=null) $('#www_studio').val($.cookie('www_studio').replace(/\+/,' '));
        if ($.cookie('osebi')!=null) $('#osebi').val($.cookie('osebi').replace(/\+/,' '));

        if($.cookie('prikaz_email')!=null && $.cookie('prikaz_email')=='1') {
            $('#prikaz_email').attr('checked','checked');
        }
        if($.cookie('prikaz_email')!=null && $.cookie('prikaz_telefon')=='1') {
            $('#prikaz_telefon').attr('checked','checked');
        }
        if($.cookie('postback')!=null) $.cookie('postback',null, '/');
}

